package com.blrs.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.annotation.RepeatSubmit;
import com.blrs.common.core.page.PageDomain;
import com.blrs.common.core.page.TableSupport;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.page.PaginationUtil;
import com.blrs.domain.EnterpriseInfo;
import com.blrs.domain.FileCabinetInformation;
import com.blrs.domain.ProjectInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.domain.ExternalDocument;
import com.blrs.service.file.IExternalDocumentService;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 外部文件Controller
 * 
 * @author ruoyi
 * @date 2023-11-01
 */
@RestController
@RequestMapping("/file/external")
public class ExternalDocumentController extends BaseController
{
    @Autowired
    private IExternalDocumentService externalDocumentService;


    @GetMapping("/listPermission")
    public TableDataInfo listPermission(ExternalDocument externalDocument)
    {
        startPage();
        List<ExternalDocument> list = externalDocumentService.selectPermissionList(externalDocument);
        return getDataTable(list);

    }

    /**
     * 查询外部文件柜树形列表
     */
    @GetMapping("/listFileTree")
    public AjaxResult listFileTree(FileCabinetInformation fileCabinetInformation)
    {
       /* Long userid = SecurityUtils.getUserId();
        if(userid!=1){

        }*/
        fileCabinetInformation.setBelongTo(SecurityUtils.getOrgId());
        List<Map> list = externalDocumentService.getFileTree(fileCabinetInformation);
        return success(list);
    }

    /**
     * 查询外部文件列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ExternalDocument externalDocument)
    {
        startPage();
        //Long userid = SecurityUtils.getUserId();
        externalDocument.setBelongTo(SecurityUtils.getOrgId());
        /*if(userid!=1){

        }*/
        List<ExternalDocument> list = externalDocumentService.selectExternalDocumentList(externalDocument);
        return getDataTable(list);
       /* for (int i = 0; i < list.size(); i++) {
           if(list.get(i).getReadOnly()==null&&list.get(i).getReadWrtie()==null){
               list.splice(i,1);

           }
        }*/
    }
    /**
     * 导出外部文件列表
     */
    @Log(title = "外部文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExternalDocument externalDocument)
    {
        List<ExternalDocument> list = externalDocumentService.selectExternalDocumentList(externalDocument);
        ExcelUtil<ExternalDocument> util = new ExcelUtil<ExternalDocument>(ExternalDocument.class);
        util.exportExcel(response, list, "外部文件数据");
    }

    /**
     * 获取外部文件详细信息
     */
    @GetMapping(value = "/{internalId}")
    public AjaxResult getInfo(@PathVariable("internalId") String internalId)
    {
        return success(externalDocumentService.selectExternalDocumentByInternalId(internalId));
    }

    /**
     * 新增外部文件
     */
    @Log(title = "外部文件", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit(interval = 1000,message = "请勿重复提交！")
    public AjaxResult add(@RequestBody ExternalDocument externalDocument)
    {
        return toAjax(externalDocumentService.insertExternalDocument(externalDocument));
    }

    /**
     * 修改外部文件
     */
    @Log(title = "外部文件", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit(interval = 1000,message = "请勿重复提交！")
    public AjaxResult edit(@RequestBody ExternalDocument externalDocument)
    {
        return toAjax(externalDocumentService.updateExternalDocument(externalDocument));
    }

    /**
     * 删除外部文件
     */
    @Log(title = "外部文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{internalId}")
    public AjaxResult remove(@PathVariable String internalId)
    {
        return toAjax(externalDocumentService.deleteExternalDocumentByInternalId(internalId));
    }
}
